<template>
    <div class="container">
        <img src="@/assets/Login.jpg" alt="描述" class="clickable-image" @click="handleClick" />
        <div class="text-container">
            <p class="password-text">Reset your password</p>
        </div>
        <div class="divider"></div>
        <div class="container-v2">
            <el-row class="message">
                <el-col :span="24">
                    输入您的邮箱，我们将发送一份验证邮件:
                </el-col>
            </el-row>
            <el-row class="message2">
                <el-col :span="24">
                    输入验证码:
                </el-col>
            </el-row>
        </div>
        <input v-model="email" type="email" placeholder="请输入邮箱" class="email-input"
            :class="{ 'error-border': emailError }" @focus="clearError('email')" />
        <button :disabled="isSending" :style="{ backgroundColor: isSending ? '#D3D3D3' : '#6ECE3C' }" @click="sendEmail"
            class="send-button"> {{ isSending ? '发送中...' : '发送邮件' }}
        </button>

        <input v-model="verificationCode" type="text" placeholder="请输入验证码" class="verification-input"
            :class="{ 'error-border': verificationError }" @focus="clearError('verification')" />
        <button @click="verifyCode" class="verify-button">验 证</button>
        <p v-if="verificationError" class="error-message">{{ errorMessage }}</p>
    </div>
</template>

<script>
import { useRouter } from 'vue-router';
import { ElMessage, ElNotification } from 'element-plus';
export default {
    data() {
        return {
            email: '',
            verificationCode: '',
            verifycode: '',
            isSending: false,
            emailError: false,
            verificationError: false,
            errorMessage: '',
            router: useRouter()
        };
    },
    methods: {
        handleClick() {
            if (this.router != null) {
                this.router.push('/login');
            }
        },

        sendEmail() {
            if (this.isSending || this.email == '') return;
            this.isSending = true;
            let url = 'http://localhost:8080/api/user/sendLoginEmailVerificationCode?email=';
            url += this.email;
            console.log(url);
            // 发送验证码
            fetch(url, {
                method: 'POST',
                headers: {
                    'Content-Type': 'text/plain',
                    'Accept': 'application/json',
                },
                body: this.email
            }).then(response => {
                if (!response.ok) {
                    throw new Error(`HTTP error! Status: ${response.status}`);
                }
                return response.json();
            }).then(data => {
                if (data['code'] == 200) {
                    this.verifycode = data['msg'];
                    console.log(data['msg']);
                    console.log('验证码发送成功'); // Debug log
                }
                else {
                    ElNotification({
                        title: 'Error',
                        message: '用户不存在',
                        type: 'error',
                    })
                    this.isSending = false;
                }
            }).catch(error => console.error('Error:', error));
        },

        verifyCode() {
            this.emailError = false;
            this.verificationError = false;
            // 检查邮箱输入框
            if (!this.email) {
                this.emailError = true;
                this.errorMessage = '';
            }

            // 检查验证码输入框
            if (!this.verificationCode) {
                this.verificationError = true;
                this.errorMessage = '';
            }

            // 如果存在错误，停止执行
            if (this.verificationCode != this.verifycode || this.emailError || this.verificationError) {
                ElMessage.error('验证码错误')
                return;
            }


            if (this.router != null) {
                ElNotification({
                    title: 'Success',
                    message: '邮箱验证通过',
                    type: 'success'
                })
                localStorage.setItem('email', this.email)
                this.router.push('/reset_password');
            }
        }
    },
    
    clearError(type) {
        if (type === 'email') {
            this.emailError = false;
        } else if (type === 'verification') {
            this.verificationError = false;
        }
    },
};

</script>

<style scoped>
.container {
    position: relative;
    width: 100%;
    height: 100vh;
    background-color: white;
    display: flex;
    align-items: center;
    justify-content: center;
}

.clickable-image {
    position: absolute;
    left: 10%;
    top: 0%;
    cursor: pointer;
    width: 8%;
    height: auto;
}

.text-container {
    position: absolute;
    top: 0%;
    text-align: center;
}

.password-text {
    font-family: 'Raleway', sans-serif;
    font-size: 36px;
    font-weight: bold;
    color: black;
}

.divider {
    position: absolute;
    width: 56%;
    top: 15%;
    height: 2px;
    background-color: #bbbbbb;
}

.email-input {
    position: absolute;
    top: 22%;
    /* 设置垂直位置 */
    left: 46%;
    /* 设置水平位置 */
    transform: translateX(-50%);
    /* 中心对齐 */
    border: 1px solid #cccccc;
    border-radius: 5px;
    padding: 10px;
    width: 20%;
}

.send-button {
    position: absolute;
    top: 22%;
    /* 设置垂直位置 */
    left: 56%;
    /* 设置水平位置 */
    border: none;
    border-radius: 5px;
    color: white;
    padding: 10px;
    cursor: pointer;
    width: 7%;
}

.verification-input {
    position: absolute;
    top: 34%;
    /* 设置垂直位置 */
    left: 36%;
    /* 设置水平位置 */
    border: 1px solid #cccccc;
    border-radius: 5px;
    padding: 10px;
    width: 27%;
}

.verify-button {
    position: absolute;
    top: 42%;
    /* 设置垂直位置 */
    left: 36%;
    /* 设置水平位置 */
    background-color: #49a7f0;
    border: none;
    border-radius: 5px;
    color: white;
    padding: 10px;
    font-size: 18px;
    cursor: pointer;
}

.error-message {
    position: absolute;
    top: 50%;
    /* 设置错误信息的垂直位置 */
    left: 50%;
    /* 设置错误信息的水平位置 */
    transform: translateX(-50%);
    color: red;
    font-size: 12px;
}

.error-border {
    border: 1px solid red;
}

.container-v2 {
    position: flex;
    width: 100%;
    height: 100vh;

}

.message {
    position: absolute;
    left: 36%;
    top: 18%;
    font-size: 16px;
    color: black;
}

.message2 {
    position: absolute;
    left: 36%;
    top: 30%;
    font-size: 16px;
    color: black;
}
</style>